package com.whut.welcomepage.fragment;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.whut.welcomepage.adapter.MainFragPagerAdapter;
import com.whut.welcomepage.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class MainFragment extends Fragment {
    String HOMEPATH = "http://piao.163.com/m/movie/list" + "" +
            ".html?app_id=2&mobileType=iPhone&ver=2.5&channel=lede&deviceId=C1985DD9-0125-4AB5" +
            "-B66B-B91A85824BBA&apiVer=11&city=110000";
    private int preIndex = 0;
    private List<Fragment> mList = new ArrayList<>();
    private RadioGroup radioGroup_mainFragment;
    private ViewPager viewPager_mainFragment;
    private Context mcontext;
    private RadioButton[] radioButtons;
    private Drawable[] iconDrawable;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mcontext = getContext();
        viewPager_mainFragment = (ViewPager) view.findViewById(R.id.viewPager_mainFragment);
        radioGroup_mainFragment = (RadioGroup) view.findViewById(R.id.radioGroup_mainFragment);
        initData();
        initUI();
    }

    private void initUI() {
        MainFragPagerAdapter adapter = new MainFragPagerAdapter(getFragmentManager(), mList);
        //        viewPager_mainFragment.setOffscreenPageLimit(2);
        viewPager_mainFragment.setAdapter(adapter);
        viewPager_mainFragment.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                radioButtons[preIndex].setTextColor(Color.GRAY);
                iconDrawable[preIndex].setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
                radioButtons[position].setTextColor(Color.parseColor("#ff669900"));
                iconDrawable[position].setColorFilter(Color.parseColor("#ff669900"), PorterDuff
                        .Mode.SRC_IN);
                preIndex = position;
            }
        });
        radioGroup_mainFragment.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener
                () {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < radioButtons.length; i++) {
                    if (radioButtons[i].getId() == checkedId) {
                        viewPager_mainFragment.setCurrentItem(i);
                    }
                }
            }
        });
        //        初始化第一次加载的数据
        //        设置radioButtons[preIndex]为选择状态，从而加载viewPager第一页的信息
        radioButtons[preIndex].setChecked(true);
        //        改变radioButtons[preIndex]的文字颜色为选择状态的颜色
        radioButtons[preIndex].setTextColor(Color.parseColor("#ff669900"));
        //        改变iconDrawable[preIndex]的图片颜色为选择状态的颜色
        iconDrawable[preIndex].setColorFilter(Color.parseColor("#ff669900"), PorterDuff.Mode
                .SRC_IN);

    }

    private void initData() {
        String[] titles = getResources().getStringArray(R.array.titles);
        TypedArray array = getResources().obtainTypedArray(R.array.titleIcons);
        radioButtons = new RadioButton[titles.length];
        iconDrawable = new Drawable[titles.length];
        for (int i = 0; i < titles.length; i++) {
            //            实例化RadioButton对象，并且为radioButtons数组赋值
            radioButtons[i] = new RadioButton(mcontext);
            //            为radioButton设置文字
            radioButtons[i].setText(titles[i]);
            //            为radioButton设置文字颜色
            radioButtons[i].setTextColor(Color.GRAY);
            //            为radioButton设置文字大小
            radioButtons[i].setTextSize(12);
            //            去除radioButton的选择圆点
            radioButtons[i].setButtonDrawable(android.R.color.transparent);
            //            去除radioButton的背景色
            radioButtons[i].setBackgroundResource(android.R.color.transparent);
            //            获取屏幕的宽度
            int widthPixels = getResources().getDisplayMetrics().widthPixels;
            //            设置radioButton的宽度
            radioButtons[i].setWidth(widthPixels / titles.length);
            //            设置radioButton文字居中
            radioButtons[i].setGravity(Gravity.CENTER);
            iconDrawable[i] = array.getDrawable(i);
            //            设置drawable的大小
            //            arrIconDrawable[i].setBounds(0, 0, arrIconDrawable[i].getMinimumWidth(),
            //            arrIconDrawable[i].getMinimumHeight());
            iconDrawable[i].setBounds(0, 0, 40, 40);
            //            设置drawable的填充色
            iconDrawable[i].setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
            //            为radioButton设置图片
            radioButtons[i].setCompoundDrawables(null, iconDrawable[i], null, null);
            //            为radioButton设置文字和图片间的间距
            radioButtons[i].setCompoundDrawablePadding(5);
            //            将radioButton添加到radioGroup中
            radioGroup_mainFragment.addView(radioButtons[i]);
            //            为ViewPager适配器的数据源List<Fragment>mList添加数据
            mList.add(BlankFragment.getInstance(i, HOMEPATH));
        }
    }
}
